#include <iostream>
#include <memory>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <cstdlib>
#include <arpa/inet.h>
#include <cstring>
#include <unistd.h>
using namespace std;
void usage(const string &proName)
{
    cout << "\n\rusage:" << proName << "serverIP serverPort" << endl;
}

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        usage(argv[0]);
        exit(1);
    }
    string serverip = argv[1];
    uint16_t serverport = stoi(argv[2]);

    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(serverport);
    inet_pton(AF_INET, serverip.c_str(), &(server.sin_addr));
    socklen_t len = sizeof(server);

    while (1)
    {
        int cnt = 10; // 重连最长时间
        int needtoreconnect = false;
        int sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0)
        {
            cerr << "socket create error" << endl;
            exit(2);
        }
        do
        {
            int ret = connect(sockfd, (struct sockaddr *)&server, len);
            if (ret < 0)
            {
                needtoreconnect = true;
                cerr << "connect error，reconnect..." << cnt << endl;
                --cnt;
                sleep(1);
            }
            else
            {
                break;
            }
        } while (cnt && needtoreconnect);
        if (cnt == 0)
        {
            cerr << "user offline..." << endl;
            break;
        }

        //while(1){
            string msg;
            cout << "Please send message: ";

            // 向服务器发送用户输入的消息
            getline(cin, msg);


            int n = write(sockfd, msg.c_str(), msg.size());
            if (n < 0)
            {
                cerr << "write error..." << endl;
                break;
            }

            // 接收服务器返回的响应内容
            char inbuffer[1024];
            n = read(sockfd, inbuffer, sizeof(inbuffer));
            if (n > 0)
            {
                inbuffer[n] = '\0';
                cout << inbuffer << endl;
             }//else{
            //     break;
            // }
        //}
        close(sockfd);
    }

    return 0;
}

// int main(int argc, char *argv[])
// {
//     if (argc != 3)
//     {
//         usage(argv[0]);
//         exit(1);
//     }
//     string serverip = argv[1];
//     uint16_t serverport = stoi(argv[2]);

//     int sockfd = socket(AF_INET, SOCK_STREAM, 0);
//     if (sockfd < 0)
//     {
//         cerr << "socket create error" << endl;
//         exit(2);
//     }

//     struct sockaddr_in server;
//     memset(&server, 0, sizeof(server));
//     server.sin_family = AF_INET;
//     server.sin_port = htons(serverport);
//     inet_pton(AF_INET, serverip.c_str(), &(server.sin_addr));
//     socklen_t len = sizeof(server);
//     int ret = connect(sockfd, (struct sockaddr *)&server, len);
//     if (ret < 0)
//     {
//         cerr << "connect error..." << endl;
//         exit(3);
//     }

//     string msg;
//     while (1)
//     {
//         // 向服务器发送用户输入的消息
//         cout << "Please send message: ";
//         getline(cin, msg);
//         write(sockfd, msg.c_str(), msg.size());

//         // 接收服务器返回的响应内容
//         char inbuffer[1024];
//         int n = read(sockfd, inbuffer, sizeof(inbuffer));
//         if (n > 0){
//             inbuffer[n] = '\0';
//             cout << inbuffer << endl;
//         }
//     }

//     return 0;
// }